package model

import (
	"fmt"
	"gowork/shoppingBodyAward/utils"
	"time"
)

type LinkageMessage struct {
	LinkageMessageId int64 `xorm:"pk autoincr"`
	MessageTypeId int
	SendAccountId int64
	SendAccountName string
	RecvAccountId int64
	RecvAccountName string
	CustomerId int64
	CustomerName string
	SendMessageBody string
	RecvMessageBody string
	CreatedAt time.Time
  	UpdatedAt time.Time
}

func (lkm *LinkageMessage)InsertLinkageMessage(lkmv *LinkageMessage) *LinkageMessage{
	x.Insert(lkmv)
	return lkmv
}

func (lkm *LinkageMessage)CreatLinkage(lkmv *LinkageMessage) map[string]string{
	var c *Customer
	var cr *CallRecord
	var o *Order
	var bh *BusinessHours
	var lkt *LinkageTask
	var a *Account
	var h time.Duration

	w := new(utils.Worker)

	rc := c.QueryCustomerById(lkmv.CustomerId)
	ra := a.QueryAccountOne(rc.FounderId)

	if lkt.QueryLinkageTaskByCustomerId(lkmv.CustomerId) == true && c.QueryCustomerByBloo(lkmv.CustomerId) == true && cr.QueryCallRecordByBool(lkmv.CustomerId) == true &&
		len(o.QueryOrderList(lkmv.CustomerId)) > 0 && bh.QueryBusinessHours() == true{
			if ra.Incumbency == 2{
				ma := a.QueryAccountById(rc.FounderId)
				lkmv.RecvAccountId = ma.AccountId
				lkmv.RecvAccountName = ma.AccountName
				h ,_ = time.ParseDuration("4h")
			}else {
				lkmv.RecvAccountId = rc.FounderId
				lkmv.RecvAccountName = rc.FounderName
				h ,_ = time.ParseDuration("2h")
			}
			rlkmv := lkm.InsertLinkageMessage(lkmv)

			var lktv = LinkageTask{
				LinkageTaskId: w.GetId(),
				CustomerId : rlkmv.CustomerId,
				CustomerName : rlkmv.CustomerName,
				AccountId : rlkmv.SendAccountId,
				LinkageMessageId : rlkmv.LinkageMessageId,
				LinkageState :1,
				EmployeeOrSupervisor : 1,
				TermOfValidity : time.Now().Add(h),
				IsOver: 1,
				CreatedAt : time.Now(),
				UpdatedAt : time.Now(),
			}
			lkt.InsertLinkageTask(&lktv)
			
		return map[string]string{"message": "成功","code":"200"}
	}
	return map[string]string{"message": "提交失败","code":"200"}
}

func (lkm *LinkageMessage)SendMessage(lkmv *LinkageMessage) map[string]string{
	x.Id(lkmv.LinkageMessageId).Update(lkmv)
	return map[string]string{"message": "成功","code":"200"}
}

func (lkm *LinkageMessage)QueryLinkageMessageById(lkmId int64) *LinkageMessage{
	lkme := new(LinkageMessage)
	x.Id(lkmId).Get(lkme)
	return lkme
}

func (lkm *LinkageMessage)ReplyMessage(lkmId int64, text string) map[string]string{
	lkmv := new(LinkageMessage)
	x.Id(lkmId).Get(lkmv)
	fmt.Println(lkmv.RecvMessageBody)
	if lkmv.RecvMessageBody == ""{
		lkmv.RecvMessageBody += text
	}else{
		lkmv.RecvMessageBody = lkmv.RecvMessageBody +","+text
	}
	_ , err := x.Id(lkmv.LinkageMessageId).Update(lkmv)
	if err != nil {
		return nil
	}
	if lkmv.MessageTypeId == 3{
		var lkt *LinkageTask
		lktv := lkt.QueryLinkageTaskById(lkmv.LinkageMessageId)
		if lktv.LinkageTaskId != 0 {
			lktv.LinkageState = 3
			lktv.ShoppingReply = time.Now()
			hh, _ := time.ParseDuration("12h")
			lktv.TermOfValidity = lktv.ShoppingReply.Add(hh)
			lkt.UpdateTermOfValidity(lktv)
		}
	}

	return map[string]string{"message": "成功","code":"200"}
}